package com.systop.controller;

import com.systop.pojo.Banner;
import com.systop.pojo.Goods;
import com.systop.pojo.User;
import com.systop.service.BannerService;
import com.systop.unti.Md5Utils;
import com.systop.unti.ResponseResult;
import org.apache.commons.io.FilenameUtils;
import org.codehaus.jettison.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author zhangnan
 * @Data 2020/8/003 上午 10:05
 * @Descriiption
 **/
@RestController
@CrossOrigin
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    //查询所有banner
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public ResponseResult selectBannerAll() {
        return bannerService.selectBannerAll();
    }

    //添加banner
    @RequestMapping(value = "/insertBanner", method = RequestMethod.POST)
    public ResponseResult insertBanner(@RequestParam("banner_name") String banner_name, @RequestParam("banner_describtion") String banner_describtion,
                                       @RequestParam(value = "file", required = false) MultipartFile[] file) throws IOException, JSONException, org.json.JSONException {
        org.json.JSONObject res = new org.json.JSONObject();
        org.json.JSONObject resUrl = new JSONObject();
        List<String> imageurls = new ArrayList<>();
        for (MultipartFile files : file) {
            String filename = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(files.getOriginalFilename());
            String filenames = filename + "." + ext;
            System.out.println(filenames);
            String pathname = "F:\\idea-workspace\\hgtt\\admin\\src\\main\\resources\\bannerimages\\" + filenames;
            files.transferTo(new File(pathname));
            imageurls.add("bannerimages/" + filenames);
        }
        resUrl.put("src", imageurls);
        res.put("data", resUrl);
        String banner_photo = resUrl.get("src").toString();
        System.out.println(banner_photo);
        System.out.println(resUrl.get("src"));
        System.out.println(res.get("data"));
        Banner banner = new Banner();
        banner.setBanner_photo(banner_photo);
        banner.setBanner_describtion(banner_describtion);
        banner.setBanner_name(banner_name);
        return bannerService.insertbanners(banner);
    }
    //更新前根据id查询banner信息
    @RequestMapping(value = "/selectBannerByid", method = RequestMethod.POST)
    public ResponseResult selectBannerByid(Integer banner_id){
        return bannerService.selectBannerByid(banner_id);
    }
    //更新banner
    @RequestMapping(value = "/updateBannerByid", method = RequestMethod.POST)
    public ResponseResult updateBannerByid(@RequestParam("banner_name") String banner_name, @RequestParam("banner_describtion") String banner_describtion,
                                           @RequestParam("banner_id") Integer banner_id,
                                           @RequestParam(value = "file", required = false) MultipartFile[] file)throws IOException, JSONException, org.json.JSONException{
            org.json.JSONObject res = new org.json.JSONObject();
            org.json.JSONObject resUrl = new JSONObject();
            List<String> imageurls = new ArrayList<>();
            for (MultipartFile files : file) {
                String filename = UUID.randomUUID().toString().replaceAll("-", "");
                String ext = FilenameUtils.getExtension(files.getOriginalFilename());
                String filenames = filename + "." + ext;
                System.out.println(filenames);
                String pathname = "F:\\idea-workspace\\hgtt\\admin\\src\\main\\resources\\bannerimages\\" + filenames;
                files.transferTo(new File(pathname));
                imageurls.add("bannerimages/" + filenames);
            }
            resUrl.put("src", imageurls);
            res.put("data", resUrl);
            String banner_photo = resUrl.get("src").toString();
            System.out.println(banner_photo);
            System.out.println(resUrl.get("src"));
            System.out.println(res.get("data"));
            Banner banner = new Banner();
            banner.setBanner_id(banner_id);
            banner.setBanner_photo(banner_photo);
            banner.setBanner_describtion(banner_describtion);
            banner.setBanner_name(banner_name);
            return bannerService.updateBannerByid(banner);

    }
    //根据id删除banner信息
    @RequestMapping(value = "/deleteBannerByid", method = RequestMethod.POST)
    public ResponseResult deleteBannerByid(Integer banner_id){
        return bannerService.deleteBannerByid(banner_id);
    }


}
